package com.ly.rotatepicbrowserdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * android 中轴旋转特效实现，制作别样的图片浏览器  from--guolin
 */
public class MainActivity extends AppCompatActivity {

    /**
     * 根布局
     */
    private RelativeLayout layout;

    /**
     * 用于展示图片列表的ListView
     */
    private ListView picListView;

    /**
     * 用于展示图片详细的ImageView
     */
    private ImageView picture;

    /**
     * 图片列表的适配器
     */
    private PictureAdapter adapter;

    /**
     * 存放所有图片的集合
     */
    private List<Picture> picList = new ArrayList<Picture>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 对图片列表数据进行初始化操作
        initPics();
        layout = (RelativeLayout) findViewById(R.id.layout);
        picListView = (ListView) findViewById(R.id.pic_list_view);
        picture = (ImageView) findViewById(R.id.picture);
        adapter = new PictureAdapter(this, 0, picList);
        picListView.setAdapter(adapter);

        picListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击某一项时，将ImageView设置为对应的图片
                picture.setImageResource(picList.get(position).getResource());
                //获取布局的中心点，作为旋转的中心点
                float centerX = layout.getWidth()/2f;
                float centerY = layout.getHeight()/2f;
                //构建3D动画
                Rotate3dAnimation rotation = new Rotate3dAnimation(0,90,centerX,centerY,310.0f,true);
                rotation.setDuration(500);
                rotation.setFillAfter(true);
                rotation.setInterpolator(new AccelerateInterpolator());

                //设置动画的监听器
                rotation.setAnimationListener(new TurnToImageView());
                layout.startAnimation(rotation);
            }
        });


        picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //获取布局的中心点，作为旋转的中心点
                float centerX = layout.getWidth()/2f;
                float centerY = layout.getHeight()/2f;
                //点击图片，图片隐藏，listview显示。
                Rotate3dAnimation rotation = new Rotate3dAnimation(360,270,centerX,centerX,310.0f,true);
                rotation.setDuration(500);
                rotation.setFillAfter(true);
                rotation.setInterpolator(new AccelerateInterpolator());
                rotation.setAnimationListener(new TurnToListView());

                layout.startAnimation(rotation);



            }
        });


    }



    private void initPics() {
        Picture bird = new Picture("H&B", R.drawable.sb1);
        picList.add(bird);
        Picture winter = new Picture("Change", R.drawable.sb2);
        picList.add(winter);
        Picture autumn = new Picture("Wedding", R.drawable.sb3);
        picList.add(autumn);
        Picture bird2 = new Picture("H&B", R.drawable.sb1);
        picList.add(bird);
        Picture winter2 = new Picture("Change", R.drawable.sb2);
        picList.add(winter);
        Picture autumn2 = new Picture("Wedding", R.drawable.sb3);
        picList.add(autumn);
        Picture bird3 = new Picture("H&B", R.drawable.sb1);
        picList.add(bird);
        Picture winter3 = new Picture("Change", R.drawable.sb2);
        picList.add(winter);
        Picture autumn3 = new Picture("Wedding", R.drawable.sb3);
        picList.add(autumn);
        Picture bird4 = new Picture("H&B", R.drawable.sb1);
        picList.add(bird);
        Picture winter4 = new Picture("Change", R.drawable.sb2);
        picList.add(winter);
        Picture autumn4 = new Picture("Wedding", R.drawable.sb3);
        picList.add(autumn);
//        Picture greatWall = new Picture("Love", R.drawable.sb4);
//        picList.add(greatWall);
//        Picture waterFall = new Picture("Family Photo", R.drawable.sb5);
//        picList.add(waterFall);

    }

    class TurnToImageView implements Animation.AnimationListener{

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            float centerX = layout.getWidth()/2f;
            float centerY = layout.getHeight()/2f;

            picListView.setVisibility(View.GONE);
            picture.setVisibility(View.VISIBLE);
            picture.requestFocus();

            Rotate3dAnimation rotation = new Rotate3dAnimation(270,360,centerX,centerX,310.0f,false);
            rotation.setDuration(500);
            rotation.setFillAfter(true);
            /**
             * AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢，在中间的时候加速

             AccelerateInterpolator  在动画开始的地方速率改变比较慢，然后开始加速

             AnticipateInterpolator 开始的时候向后然后向前甩

             AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值

             BounceInterpolator   动画结束的时候弹起

             CycleInterpolator 动画循环播放特定的次数，速率改变沿着正弦曲线

             DecelerateInterpolator 在动画开始的地方快然后慢

             LinearInterpolator   以常量速率改变

             OvershootInterpolator    向前甩一定值后再回到原来位置
             */
            rotation.setInterpolator(new AccelerateInterpolator());
            layout.startAnimation(rotation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }


    /**
     * 点击picture imageview消失 listview显示
     */
    class TurnToListView implements Animation.AnimationListener{

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // 获取布局的中心点位置，作为旋转的中心点
            float centerX = layout.getWidth() / 2f;
            float centerY = layout.getHeight() / 2f;
            // 将ImageView隐藏
            picture.setVisibility(View.GONE);
            // 将ListView显示
            picListView.setVisibility(View.VISIBLE);
            picListView.requestFocus();
            // 构建3D旋转动画对象，旋转角度为90到0度，这使得ListView将会从不可见变为可见，从而回到原点
            final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,
                    310.0f, false);
            // 动画持续时间500毫秒
            rotation.setDuration(500);
            // 动画完成后保持完成的状态
            rotation.setFillAfter(true);
            rotation.setInterpolator(new AccelerateInterpolator());
            layout.startAnimation(rotation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }

}
